package com.tech.zhidengxian.service.impl;

import com.tech.zhidengxian.mapper.UserMapper;
import com.tech.zhidengxian.model.dataobject.UserDO;
import com.tech.zhidengxian.model.dto.UserDTO;
import com.tech.zhidengxian.service.api.UserService;
import com.tech.zhidengxian.util.JacksonUtils;
import com.tech.zhidengxian.util.TimeUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public List<UserDTO> queryUserList() {
        List<UserDO> userDOS = userMapper.queryUserList();
        List<UserDTO> userDTOList = JacksonUtils.convertList(userDOS, UserDTO.class);
        return userDTOList;
    }

    @Override
    public UserDTO pwdLogin(UserDTO userDTO) {
        UserDO userDO = JacksonUtils.convert(userDTO,UserDO.class);
        UserDO login = userMapper.pwdLogin(userDO);
        return JacksonUtils.convert(login,UserDTO.class);
    }

    @Override
    public boolean register(UserDTO userDTO) {
        userDTO.setRegisterTime(TimeUtil.currentDate());
        userDTO.setUpdateTime(TimeUtil.currentDate());
        UserDO userDO = JacksonUtils.convert(userDTO,UserDO.class);

        return userMapper.register(userDO)>0;
    }

    @Override
    public int updateById(UserDTO userDTO) {
        UserDO userDO = JacksonUtils.convert(userDTO, UserDO.class);
        return userMapper.updateUserById(userDO);
    }

    @Override
    public UserDTO queryById(Long uid) {
        UserDO userDO = userMapper.selectById(uid);
        return JacksonUtils.convert(userDO,UserDTO.class);
    }


}
